package com.kongwg.java1;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 解决线程安全问题的方式三：lock锁  --->jdk5.0之后的
 *
 * @author kongwg
 * @create 2020-12-05 17:21
 */

class window implements Runnable{
    private int ticket = 100;
    private ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true){

            try {
                lock.lock();
                if (ticket > 0){
                    System.out.println(Thread.currentThread().getName() + "卖的票号为：" + ticket);
                    ticket--;
                }else {
                    break;
                }
            } finally {
                lock.unlock();
            }

        }
    }
}
public class LockTest {
    public static void main(String[] args) {
        window w = new window();
        Thread t1 = new Thread(w);
        Thread t2 = new Thread(w);
        Thread t3 = new Thread(w);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}
